﻿Public Class NotificationWindowForm

#Region " Variables "

    Dim showing As Boolean
    Private m_CurrentForegroundWindow As IntPtr 'The handle of the window that currently has focus.

#End Region 'Variables

#Region " APIs "

    ' -----------------------------------------------------------------------------
    ' <summary>
    '     Gets the handle of the window that currently has focus.
    ' </summary>
    ' <returns>
    '     The handle of the window that currently has focus.
    ' </returns>
    ' -----------------------------------------------------------------------------
    Private Declare Auto Function GetForegroundWindow Lib "user32" () As IntPtr

    ' -----------------------------------------------------------------------------
    ' <summary>
    '     Activates the specified window.
    ' </summary>
    ' <param name="hWnd">
    '     The handle of the window to be focused.
    ' </param>
    ' <returns>
    '     True if the window was focused;
    '     False otherwise.
    ' </returns>
    ' -----------------------------------------------------------------------------
    Private Declare Auto Function SetForegroundWindow Lib "user32" (ByVal hWnd As IntPtr) As Boolean

#End Region 'APIs

#Region " Methods "

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    '''     Displays the form.
    ''' </summary>
    ''' <remarks>
    '''     Required to allow the form to determine the current foreground window
    '''     before being displayed.
    ''' </remarks>
    ''' -----------------------------------------------------------------------------
    Public Shadows Sub Show()
        'Determine the current foreground window so it can be reactivated each time this form tries to get the focus.
        Me.m_CurrentForegroundWindow = NotificationWindowForm.GetForegroundWindow()

        'Display the form.
        MyBase.Show()
    End Sub

#End Region 'Methods

#Region " Event Handlers "

    Private Sub NotificationWindowForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        NotificationWindowForm.SetForegroundWindow(Me.m_CurrentForegroundWindow)
    End Sub

    Private Sub NotificationWindowForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim screenWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
        Dim screenHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
        Me.Left = screenWidth - Me.Width
        Me.Top = screenHeight - Me.Height
        TimerClose.Interval = (My.Settings.sinetNotification * 1000)
        Me.Opacity = 0.0
        showing = True
        TimerFade.Start()
    End Sub

    Private Sub TimerClose_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerClose.Tick
        Me.Close()
    End Sub

    Private Sub TimerFade_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerFade.Tick
        If showing Then
            If Not Me.Opacity = 1 Then
                Me.Opacity += 0.1
            Else
                ''This means we are done fading in
                TimerFade.Stop()
            End If
        Else
            If Not Me.Opacity = 0.0 Then
                Me.Opacity -= 0.1
            Else
                ''This means we are done fading out
                Me.Dispose()
            End If
        End If
    End Sub

    Private Sub NotificationWindowForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        showing = False
        TimerFade.Start()
    End Sub

#End Region 'Event Handlers

End Class